package com.emoriarty.android.outilsresources;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.text.Html;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.emoriarty.android.outilsresources.anim.button.ButtonDeployableTopActivity;
import com.emoriarty.android.outilsresources.lists.overlaping.OverlapLayoutsActivity;
import com.emoriarty.android.outilsresources.lists.statesselector.StatesSelectorActivity;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        boolean loadGrid;
        
        setContentView(R.layout.activity_main);
        
        //Getting metrics
        DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        
        //Setting the device values
        String modelText = "Model: <b>" + android.os.Build.MODEL + "</b>" 
        		+ " Brand: <b>" + android.os.Build.BRAND + "</b>"
        		+ " Product: <b>" + android.os.Build.PRODUCT + "</b>"
        		+ " Device: <b>" + android.os.Build.DEVICE + "</b>"
        		+ " Hardware: <b>" + android.os.Build.HARDWARE + "</b>"
        		+ " Manufacturer: <b>" + android.os.Build.MANUFACTURER + "</b>";
        TextView model = (TextView) findViewById(R.id.model);
        model.setText(Html.fromHtml(modelText));
        
        //Checking the screen size
        String size = "";
        switch (getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) {
        case Configuration.SCREENLAYOUT_SIZE_SMALL:
			size = "Small";
			loadGrid = false;
			break;
        case Configuration.SCREENLAYOUT_SIZE_NORMAL:
			size = "Normal";
			loadGrid = false;
			break;
		case Configuration.SCREENLAYOUT_SIZE_LARGE:
			size = "Large";
			loadGrid = true;
			break;
		case Configuration.SCREENLAYOUT_SIZE_XLARGE:
			size = "Xlarge";
			loadGrid = true;
			break;
		case Configuration.SCREENLAYOUT_SIZE_UNDEFINED:
			size = "Undefined";
			loadGrid = false;
			break;
		default:
			size = "??????";
			loadGrid = false;
			break;
		}
        
        String metricsText = "Density: <b>" + displaymetrics.densityDpi + "</b>"
        		+ " Dimensions: <b>" + displaymetrics.widthPixels + " x " + displaymetrics.heightPixels + "</b>"
        		+ " Scaled Density: <b>" + displaymetrics.scaledDensity + "</b>"
        		+ " Screen layout size: <b>" + size + "</b>";
        TextView density = (TextView) findViewById(R.id.metrics);
        density.setText(Html.fromHtml(metricsText));
        
        if (loadGrid) {
        	settingGrid();
        } else {
        	settingList();
        }
        	
    }
    
    private void settingGrid() {
    	//Setting the GridView
        GridView gridMenu = (GridView) findViewById(R.id.grid_menu);
        
        gridMenu.setAdapter(new ImageAdapter());
        gridMenu.setOnItemClickListener(listener);
    }
    
    private void settingList() {
    	ListView listView = (ListView) findViewById(R.id.list_menu);
    	String[] samples = new String[3];
    	samples[0] = getResources().getString(R.string.overlaping_title);
    	samples[1] = getResources().getString(R.string.sliding_button);
    	samples[2] = getResources().getString(R.string.states_selector);
    	listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, samples));
    	
    	listView.setOnItemClickListener(listener);
    }
    
    /**
     * This listener chose the activity to show depending on the position selected.
     */
    OnItemClickListener listener = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            switch (position) {
			case 0: intentTo(OverlapLayoutsActivity.class); break;
			case 1: intentTo(ButtonDeployableTopActivity.class); break;
			case 2: intentTo(StatesSelectorActivity.class); break;
			default: break;
			}          
        }
	};
    
    /**
     * This method makes an intent another activity just passing as argument the activity
     */
    private void intentTo(Class<?> actv) {
    	Intent intent = new Intent(this, actv);
    	startActivity(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    
    public class ImageAdapter extends BaseAdapter {

        public ImageAdapter() {
        	super();
        }

        public int getCount() {
            return mThumbIds.length;
        }

        public Object getItem(int position) {
            return mThumbIds[position];
        }

        public long getItemId(int position) {
            return 0;
        }

        // create a new ImageView for each item referenced by the Adapter
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {  // if it's not recycled, initialize some attributes
            	LayoutInflater inflater = getLayoutInflater();
            	convertView = inflater.inflate(R.layout.grid_item, null);
            }

            GridItem item = (GridItem) getItem(position);
            ((ImageView) convertView.findViewById(R.id.icon)).setImageResource(item.getDrawable());
            ((TextView) convertView.findViewById(R.id.name)).setText(item.getText());
            
            return convertView;
        }

        // references to our images
        private GridItem[] mThumbIds = {
                new GridItem(R.drawable.overlap, getResources().getString(R.string.overlaping_title)),
                new GridItem(R.drawable.overlap, getResources().getString(R.string.sliding_button)),
                new GridItem(R.drawable.overlap, getResources().getString(R.string.states_selector))
        };
    }
    
    class GridItem {
    	private int drawable;
    	private String text;
    	
    	GridItem(int drawable, String text) {
			this.drawable = drawable;
			this.text = text;
		}
    	
    	int getDrawable() {
    		return drawable;
    	}
    	
    	String getText() {
    		return text;
    	}
    }
}
